require("../css/login.less")
//获取表单元素节点
const user = document.querySelector("#user");
const pwd = document.querySelector("#pwd");
const sumbitbtn = document.querySelector("#sumbitbtn")
const hint = document.querySelector("#hint");

// 点击登录按钮进行验证并向后台发送ajax请求
sumbitbtn.addEventListener("click", function () {
    // 获取表单元素节点里的值
    const userVal = user.value;
    const pwdVal = pwd.value;
    //对账号以及密码进行合法效验，效验通过就向后台发送ajax请求
    if (userVal === "") { //账户验证
        hint.textContent = "账号不能为空";
        return;
    } else if (!window.utils.isPhoneNumber(userVal)) {
        hint.textContent = "账号错误";
        return;
    } else if (pwdVal === "") {
        hint.textContent = "密码不能为空";
        return;
    } else if (!window.utils.isPassword(pwdVal)) {
        hint.textContent = "密码错误";
        return;
    } else {
        //上面所有验证都通过后向后台发送ajax请求
        window.$http.post('users/login', {
            "account": userVal,
            "password": pwdVal
        }, function (res) {
            if (res.status === 0) {
                // 成功提示
                window.utils.showTost("succ", "登陆成功", 1000)
                // 将获取到账户信息保存到本地存储
                window.localStorage.setItem("userId", res.data.user.userId)
                // 成功
                setTimeout(function () {
                    window.location.href = "./home.html"
                }, 1000)

            } else {
                // 失败
                window.utils.showTost("err", res.msg, 1000)
            }
        })
    }
})